package cn.tedu.hotel_management_system.service.impl;

import cn.tedu.hotel_management_system.base.exception.HotelManagementException;
import cn.tedu.hotel_management_system.base.json_result.ResultDataDto;
import cn.tedu.hotel_management_system.base.json_result.ResultDto;
import cn.tedu.hotel_management_system.mapper.RoomsMapper;
import cn.tedu.hotel_management_system.pojo.dto.RoomMangerListDto;
import cn.tedu.hotel_management_system.pojo.dto.RoomUpdateStatusIn;
import cn.tedu.hotel_management_system.pojo.entity.Rooms;
import cn.tedu.hotel_management_system.service.RoomManagementService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

import static cn.tedu.hotel_management_system.base.constant.DataBaseConstants.DATABASE_AFFECTED_ROW;
import static cn.tedu.hotel_management_system.base.constant.DataBaseConstants.DATABASE_RESULT_SIZE_ONE;

/**
 * @author dkk
 * @date 2022/8/31
 * @desc
 */
@Slf4j
@Service
public class RoomManagementServiceImpl implements RoomManagementService {

    @Autowired(required = false)
    private RoomsMapper roomsMapper;

    /**
     * 客房列表查询
     * @return json
     * @param page 起始页数
     * @param size 分页大小
     * @param roomMangerListDto 筛选查询值传递对象
     */
    @Override
    public ResultDataDto<Rooms> list(Integer page, Integer size, RoomMangerListDto roomMangerListDto) {

        // 创建查询wrapper
        LambdaQueryWrapper<Rooms> roomsQueryWrapper = new LambdaQueryWrapper<>();

        // 动态拼接查询条件
        roomsQueryWrapper.eq(
                StringUtils.isNoneBlank(roomMangerListDto.getRoomStatus()),Rooms::getRoomStatus,roomMangerListDto.getRoomStatus()) // 房间状态
                .like(StringUtils.isNoneBlank(roomMangerListDto.getRoomNum()),Rooms::getRoomNum,roomMangerListDto.getRoomNum()) // 房间号
                .eq((roomMangerListDto.getRoomTypeId())!=null,Rooms::getRoomTypeId,roomMangerListDto.getRoomTypeId()); // 房间类型

        // 执行sql分页查询
        Page<Rooms> roomsPage = roomsMapper.selectPage(new Page<>(page, size), roomsQueryWrapper);

        // 封装返回值
        List<Rooms> records = roomsPage.getRecords();
        ResultDataDto<Rooms> resultDataDto = new ResultDataDto<>();

        if (CollectionUtils.isEmpty(records)){
            resultDataDto.setMes("查询失败,无酒店房间信息");
            resultDataDto.setIsSuccess(false);
        }

        resultDataDto.setDataList(records);
        resultDataDto.setMes("查询成功");
        resultDataDto.setIsSuccess(true);
        return resultDataDto;
    }

    @Transactional
    @Override
    public ResultDto<Void> updateStatus(RoomUpdateStatusIn roomUpdateStatusIn) {
        ResultDto<Void> resultDto = new ResultDto<>();
        log.info("变更房间状态,入参信息为:[{}]",roomUpdateStatusIn);

        // 先查询房间是否存在,以及房间编号是否只对应一个房间
        LambdaQueryWrapper<Rooms> roomsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 获取房间编号
        String roomNum = roomUpdateStatusIn.getRoomNum();
        LambdaQueryWrapper<Rooms> queryWrapper = roomsLambdaQueryWrapper.eq(true, Rooms::getRoomNum, roomNum);
        List<Rooms> rooms = roomsMapper.selectList(queryWrapper);

        if (rooms.size()<DATABASE_RESULT_SIZE_ONE){
            log.warn("根据房间编号id:[{}]未能查询到房间", roomNum);
            HotelManagementException.cast("不存在房间编号为"+roomNum+"的房间");
        }else if (rooms.size()>DATABASE_RESULT_SIZE_ONE){
            log.warn("根据房间编号id:[{}]查询到多个房间,请检查数据配置", roomNum);
            HotelManagementException.cast("房间编号为"+roomNum+"的房间存在多个,请保证房间编号的唯一性");
        }


        // 执行更新
        LambdaUpdateWrapper<Rooms> roomsUpdateWrapper = new LambdaUpdateWrapper<>();
        roomsUpdateWrapper.eq(Rooms::getRoomNum, roomNum);
        roomsUpdateWrapper.set(Rooms::getRoomStatus,roomUpdateStatusIn.getRoomStatus());
        int update = roomsMapper.update(null, roomsUpdateWrapper);

        if (update!=DATABASE_AFFECTED_ROW){
            HotelManagementException.cast("更新失败,请稍后重试");
        }
        resultDto.setIsSuccess(true);
        return resultDto;
    }

}
